package task

import (
	"errors"

	"cvevulner/common"
	"cvevulner/taskhandler"

	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/logs"
)

// Synchronize excel data
func SynchronizeExcelData() error {
	defer common.Catchs()
	logs.Info("Start the task of generating cve raw data into cve library.")
	// Query the cve to be processed, 1: add; 2: modify
	BConfig, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		logs.Error("SynchronizeExcelData, config init, error: ", err)
		return err
	}
	// The amount of data processed at a time
	prcNum, err := BConfig.Int("crontab::prcnum")
	if err != nil {
		logs.Error("SynchronizeExcelData, crontab::prcnum, error:  ", err)
		return errors.New("value is nil")
	}
	// How many days have been processed to date data
	days, ok := BConfig.Int("crontab::days")
	if ok != nil {
		logs.Error("SynchronizeExcelData, crontab::days, err:", err)
		return ok
	}
	// openeuler Number start value
	cveRef := BConfig.String("cve::cveref")
	openeulerNum, ok := BConfig.Int("cve::openeulernum")
	if ok != nil {
		logs.Error("SynchronizeExcelData, cve::openeulernum, err:", err)
		return ok
	}
	// Processing excel raw data
	okx, err := taskhandler.GetCveOriginExcelData(prcNum, days, openeulerNum, cveRef)
	if !okx {
		logs.Error("SynchronizeExcelData, GetCveOriginExcelData, err: ", err)
		return err
	}
	return nil
}

func HandUrgentData() error{
	// Synchronize excel data
	syErr := SynchronizeExcelData()
	// Create cve data
	cErr := SyncCreateIssue(nil)
	logs.Info(syErr, cErr)
	return nil
}
